/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package geometry;

/**
 *
 * @author Eric-Standard
 */
public class Vector2d {

    public double x, y;

    /**
     * Makes a vector from at <0, 0>.
     */
    public Vector2d() {
        this(0, 0);
    }

    /**
     * Makes a vector from the specified coordinates.
     *
     * @param x
     * @param y
     */
    public Vector2d(double x, double y) {
        this.x = x;
        this.y = y;
    }

    /**
     * Makes a vector from another vector.
     *
     * @param v
     */
    public Vector2d(Vector2d v) {
        this.x = v.x;
        this.y = v.y;
    }

    /**
     * Gets the angle between this vector and another vector.
     *
     * @param v The other vector.
     * @return The angle.
     */
    public double angle(Vector2d v) {
        return Math.acos(dot(v) / (length() * v.length()));
    }

    /**
     * Gets the length of this vector.
     *
     * @return The length of this vector.
     */
    public double length() {
        return Math.pow(x * x + y * y, .5);
    }

    /**
     * Normalizes this vector in place.
     */
    public void normalize() {
        double length = length();
        if (length == 0) {
            return;
        }
        x = x / length;
        y = y / length;
    }

    /**
     * Gets the dot product between this vector and another vector.
     *
     * @param v
     * @return
     */
    public double dot(Vector2d v) {
        double dotProduct = x * v.x + y * v.y;
        return dotProduct;
    }

    public void scale(double r) {
        x *= r;
        y *= r;
    }

    public Vector2d add(Vector2d v) {
        return new Vector2d(x + v.x, y + v.y);
    }

    public Vector2d subtract(Vector2d v) {
        return new Vector2d(x - v.x, y - v.y);
    }

    public void negate() {
        x = -x;
        y = -y;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof Vector2d) {
            return false;
        }
        Vector2d v = (Vector2d) other;
        return x == v.x && y == v.y;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
        return hash;
    }
}
